package com.vip.iot.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * 易流云接口URL参数和基地址的获取
 * @auther wgb
 * @Date 2019/3/7
 */
public class CommConfig {
    public static String AppKey = "6abd03e8-14c4-4c5f-8def-32e870caf8c6";
    public static String AppSecret = "f72028d0-6553-48d9-968b-ff6169ce4421";
    public static String MotionBaseURL = "http://api.e6gps.com/public/v3/Inface/Call";
    public static String ReportBaseURL = "http://api.e6gps.com/public/v3/StatisticsReport/Call";
    public static String VehicleMap = "http://api.e6gps.com/public/v3/MapServices/Orientation.aspx?";
    public static String LoginE6WebGisUrl = "http://login.e6gps.com/Home/LoginWithToken?";
    public static String PersonSafeBaseURL = "http://117.48.196.148/position_sdk/ModularUser/User";
    public static String PersonSafeName = "admin";
    public static String PersonSafePassword = "admin";
    public static String PersonHttpVersion = "1.4.0";
    public static String Format = "json";


    /**
     * 获取系统当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss时间格式
     */
    public static String GetNowTime() {
        Date dt = new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
        String nowTime = df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy-MM-dd HH:mm:ss格式显示
        return nowTime;
    }

    /**
     * 获取签名
     * @param map 除了密钥外其他的参数集合
     * @return 排序后的map转换成的字符串
     */
    public static String GetSign(Map<String, String> map) {
        //将所有的键放在一个collection进行排序，是为了生成签名用到
        ArrayList<String> keyList = new ArrayList<>();
        for (String dataKey : map.keySet()) {
            keyList.add(dataKey);
        }
        //对键进行排序
        Collections.sort(keyList);
        //根据键再从map当中获取值，拼接成字符串
        StringBuilder sb = new StringBuilder();
        for (String key : keyList) {
            String value = map.get(key);
            if (value != null) {
                sb.append(key);
                sb.append(value);
            }
        }
        return Sign(sb.toString());
    }

    /**
     * 按照密钥+GetSign()返回值+密钥这种规则进行MD5加密
     *
     * @param pPrestr
     * @return 加密后得到的结果
     */
    public static String Sign(String pPrestr) {
        pPrestr = AppSecret + pPrestr + AppSecret;
        String md5Str = MD5Util.str2MD5(pPrestr);
        return md5Str.toUpperCase();
    }
}